package org.example;

import java.util.HashMap;
import java.util.Map;


public class DBProxy implements Dao{
    private Map<Integer, String> cache = new HashMap<>();
    private DataSupplier dataSupplier;

    DBProxy(DataSupplier dataSupplier){
        this.dataSupplier = dataSupplier;
    }

    public String getData(Integer index){
        String data = null;
        if(!cache.containsKey(index)){
            System.out.println("缓存中没有...");
            data = dataSupplier.getData(index);
            cache.put(index, data);
        }else {
            System.out.println("缓存中有!");
            data = cache.get(index);
        }
        return data;
    }
}
